import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:wan_android_flutter/pages/auth/auth_vm.dart';
import 'package:wan_android_flutter/routes/RouteUtils.dart';
import 'package:wan_android_flutter/routes/routes.dart';
import 'package:wan_android_flutter/common_ui/common_style.dart';
class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return LoginPageState();
  }
}

class LoginPageState extends State<LoginPage> {

  AuthViewModel authViewModel =AuthViewModel();
  TextEditingController? userTextEditingController;
  TextEditingController? pwdTextEditingController;
@override
  void initState() {
    // TODO: implement initState
    super.initState();
    userTextEditingController = TextEditingController();
    pwdTextEditingController = TextEditingController();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ChangeNotifierProvider(create: (context){
      return authViewModel;
    },child: Scaffold(
        backgroundColor: Colors.teal,
        body: Container(
          padding: EdgeInsets.only(left: 20.w, right: 20.w),
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              commonInput("账号",controller: userTextEditingController),
              SizedBox(
                height: 20.h,
              ),
              commonInput("密码",obscureText: true,controller: pwdTextEditingController),
              SizedBox(
                height: 50.h,
              ),
              whiteBorderButton("开始登录",onTap: (){
                authViewModel.setLoginInfo(name: userTextEditingController?.text, password:pwdTextEditingController?.text);
                authViewModel.login().then((onValue){
                  RouteUtils.pushNamedAndRemoveUntil(context,RoutesPath.tab);
                });
              }),
              _loginButton((){
                RouteUtils.pushForNamed(context, RoutesPath.registerPage);
              })
            ],
          ),
        )),);
  }
  Widget _loginButton(GestureTapCallback? onTap){
    return GestureDetector(
        onTap: onTap,
        child: Container(
          width: 100.w,
          height: 45.h,
          alignment: Alignment.center,
          child: Text(
            "注册",
            style: TextStyle(color: Colors.white, fontSize: 13.sp),
          ),
        ));
  }
}
